package com.jyxd.web.controller.basic;

import com.jyxd.web.data.basic.DrainageTubeRecord;
import com.jyxd.web.data.dto.HttpResult;
import com.jyxd.web.mq.HisViewProduction;
import com.jyxd.web.service.assessment.basic.DrainageTubeRecordService;
import com.jyxd.web.service.hisview.dto.HisViewDataDto;
import com.jyxd.web.service.patient.PatientService;
import com.jyxd.web.util.HttpCode;
import com.jyxd.web.util.UUIDUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.Map;
import java.util.Objects;

import static com.jyxd.web.config.ConstantConfig.HIS_VIEW_EVENT_DC_ICU_DAT_BRXXJLK;

@Api(tags = "引流管插拔记录接口")
@Controller
@RequestMapping(value = "/drainageTubeRecord")
public class DrainageTubeRecordController {

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private DrainageTubeRecordService drainageTubeRecordService;
    @Autowired
    private PatientService patientService;

    @Autowired
    private HisViewProduction hisViewProduction;

    @ApiOperation("新增一条插拔管记录")
    @RequestMapping(value = "/insert", method = RequestMethod.PUT)
    @ResponseBody
    public String insert (@RequestBody DrainageTubeRecord drainageTubeRecord) {
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("data", new ArrayList<>());
        json.put("msg", "添加失败");
        drainageTubeRecord.setId(UUIDUtil.getUUID());
        drainageTubeRecord.setCreateTime(new Date());
        drainageTubeRecord.setStatus(1);
        drainageTubeRecordService.insert(drainageTubeRecord);
        // 如果新增患者为重症科室患者时，新增数据中心患者基本信息 插拔管记录基本信息
        hisViewProduction.put(new HisViewDataDto(HIS_VIEW_EVENT_DC_ICU_DAT_BRXXJLK, patientService.queryData(drainageTubeRecord.getPatientId())));
        json.put("code", HttpCode.OK_CODE.getCode());
        json.put("msg", "添加成功");
        return json.toString();
    }


    @ApiOperation("根据id查询一条插拔管记录")
    @RequestMapping(value = "/queryData", method = RequestMethod.GET)
    @ResponseBody
    public HttpResult queryData (@RequestParam(name = "id") String id) {
        DrainageTubeRecord drainageTubeRecord = drainageTubeRecordService.queryData(id);
        if (Objects.isNull(drainageTubeRecord))
            return new HttpResult(200, "查询失败");
        return new HttpResult(200, drainageTubeRecord, "查询成功");
    }


    @ApiOperation("修改一条插拔管记录")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public HttpResult update (@RequestBody DrainageTubeRecord drainageTubeRecord) {
        return drainageTubeRecordService.update(drainageTubeRecord);
    }

    @ApiOperation("查询插拔管记录列表（可分页）")
    @RequestMapping(value = "/queryList", method = RequestMethod.POST)
    @ResponseBody
    public HttpResult queryList (@RequestBody Map<String, Object> map) {
        return drainageTubeRecordService.queryList(map);
    }

    @RequestMapping(value = "/deleteData", method = RequestMethod.POST)
    @ResponseBody
    public HttpResult deleteData (@RequestBody(required = false) Map<String, Object> map) {
        return drainageTubeRecordService.deleteData(map);
    }
}
